<?php
declare (strict_types=1);

namespace app\month\controller;

use app\api\server\Token;
use app\month\model\Category;
use app\month\model\Comment;
use app\month\model\Like;
use app\month\server\Baidu;
use think\Exception;
use think\facade\Db;
use think\Request;

class Article
{
    /**
     * 列表
     */
    public function list(): \think\response\Json
    {
        try {

            $cate_id = \request()->get('cate_id');

            $uid = Token::verifyToken();

            if (empty($cate_id)) {
                throw new Exception("参数错误", 1001);
            }

            $data = \app\month\model\Article::where("cate_id", $cate_id)->paginate(10)->toArray();

            $article_ids = array_column($data['data'],'id');
            # 后端数据处理、告诉前端是否点赞
            $userLike = [];
            if ($uid) {
                $userLike = Like::where('user_id','=',$uid)->select()->toArray();
                $userLike = array_column($userLike,'article_id',NULL);
            }

            // 查询点赞数
            $like = Like::field('article_id,count(user_id) as cnt')->whereIn('article_id',$article_ids)->group('article_id')->select();
            $like = $like ? $like->toArray() : [];

            $like = array_column($like,'cnt','article_id');
            // 查询评论数
            $comment = Comment::field('article_id,count(id) as cnt')->whereIn('article_id',$article_ids)->group('article_id')->select();
            $comment = $comment ? $comment->toArray() : [];

            $comment = array_column($comment,'cnt','article_id');


            foreach ($data['data'] as $key => $value){
                $data['data'][$key]['create_at'] = date("Y-m-d H:i:s",$value['create_at']);
                if (isset($like[$value['id']])) {
                    $data['data'][$key]['like_count'] = $like[$value['id']];
                } else {
                    $data['data'][$key]['like_count'] = 0;
                }
                if (isset($comment[$value['id']])) {
                    $data['data'][$key]['comment_count'] = $comment[$value['id']];
                } else {
                    $data['data'][$key]['comment_count'] = 0;
                }
                if (in_array($value['id'],$userLike)) {
                    # 点过赞
                    $data['data'][$key]['is_like'] = true;
                } else {
                    # 没有点过赞
                    $data['data'][$key]['is_like'] = false;
                }
            }


            return success($data);

        } catch (\Exception $exception) {
            return fail($exception->getCode(), $exception->getMessage());
        }
    }

    /**
     * 文章详情
     * @param $id
     */
    public function detail($id): \think\response\Json
    {
        try {


            if (empty($id)) {
                throw new Exception("参数错误", 1001);
            }
            $data = \app\month\model\Article::with('user')->find($id);
            if ($data) {
                $comment = Comment::with('user')->where('article_id','=',$id)->order('id','DESC')->select();
                $comment = $comment ? $comment->toArray() : [];
                $data['comment'] = parent_data($comment);
            }

            return success($data);

        } catch (\Exception $exception) {
            return fail($exception->getCode(), $exception->getMessage());
        }
    }

    /**
     * @return \think\response\Json
     */
    public function cateList(): \think\response\Json
    {
        try {

            $data = Category::select();

            return success($data);

        } catch (\Exception $exception) {
            return fail($exception->getCode(), $exception->getMessage());
        }
    }


    /**
     * 点赞
     */
    public function like(): \think\response\Json
    {
        try {

            $uid = \request()->uid;

            $article_id = \request()->get('article_id');

            if (empty($article_id) || !is_numeric($article_id)) {
                throw new Exception("参数错误", 1001);
            }

            $is_like = Like::where("user_id", '=', $uid)->where("article_id", '=', $article_id)->find();
            if ($is_like) {
                // 取消点赞
                Like::where("user_id", '=', $uid)->where("article_id", '=', $article_id)->delete();
                $type = 1;// 取消点赞
            } else {
                // 点赞
                Like::create([
                    'user_id' => $uid,
                    'article_id' => $article_id
                ]);
                $type = 2;// 点赞
            }

            return success(['type' => $type]);


        } catch (\Exception $exception) {
            return fail($exception->getCode(), $exception->getMessage());
        }
    }


    /**
     * 评论接口
     */
    public function comment(): \think\response\Json
    {
        $uid = \request()->uid;

        try {

            $article_id = \request()->post('article_id');
            $content = \request()->post('content');
            $pid = \request()->post('pid', 0);

            if (empty($article_id) || !is_numeric($article_id) || empty($content)) {
                throw new Exception("参数错误", 1001);
            }

            # 评论的内容是否含有敏感词语、含有敏感词语要进行过滤
            Baidu::auditingContent($content);

            // 点赞
            Comment::create([
                'user_id' => $uid,
                'content' => $content,
                'article_id' => $article_id,
                'pid' => $pid
            ]);

            return success();


        } catch (\Exception $exception) {
            return fail($exception->getCode(), $exception->getMessage());

        }


    }
}
